package ioStream.file.nodeStream.characterStream;

import org.junit.jupiter.api.Test;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:字符输出流学习
 * User:22954
 * Date:2024-03-15
 * Time:18:24
 */
@SuppressWarnings("all")
public class FileWriter_ {
    /*
    FileWriter extends OutputStreamWriter extends Writer
    FileWriter所有方法继承OutputStreamWriter继承/实现Writer
    =====================================================================================
    FileWriter(String fileName) throws IOException  使用默认字符集构造给定文件名的FileWriter
    FileWriter(String fileName, boolean append) throws IOException  根据append指示是否使用默认字符集附加写入的数据----写文件时是否追加
    =====================================================================================
    void write(int c) throws IOException 写入单个字符
    void write(char[] cbuf) throws IOException  写入指定字符数组
    void write(char[] cbuf, int off, int len) throws IOException  写入指定数组的指定部分
    void write(String str) throws IOException  写入整个字符串
    void write(String str, int off, int len) throws IOException  写入字符串的指定部分
    =====================================================================================
    write()方法只将数据写入输出流中,并未真正写入路径文件中
    数据写入路径文件在调用close()/flush()方法时,底层调用了FileOutputStream的write()方法
     */
    @Test
    public void writeFile1(){//write(int c)
        String filePath = "f:/Test/world.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write('B');
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Test
    public void writeFile2(){//write(char[] cbuf)
        String filePath = "f:/Test/world.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write("hello world".toCharArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Test
    public void writeFile3(){//write(char[] cbuf, int off, int len)
        String filePath = "f:/Test/world.txt";
        FileWriter fileWriter = null;
        char[] arr = new char[]{'a', 'b', 'c'};
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write(arr, 0, arr.length);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Test
    public void writeFile4(){//write(String str)
        String filePath = "f:/Test/world.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write("Hello world");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Test
    public void writeFile5(){//write(String str, int off, int len)
        String filePath = "f:/Test/world.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write("Hello World", 0, 5);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
